<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>牛列表</title>
	<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}">
	<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}">
	<link rel="stylesheet" type="text/css" th:href="@{/easyui/css/demo.css}">
	<script type="text/javascript" th:src="@{/easyui/jquery.min.js}"></script>
	<script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script>
	<script type="text/javascript" th:src="@{/easyui/js/validateExtends.js}"></script>
	<script type="text/javascript" th:inline="javascript">
		/*<![CDATA[*/
		$(function() {
			//datagrid初始化
			$('#dataList').datagrid({
				title:'牛列表',
				iconCls:'icon-more',//图标
				border: true,
				collapsible:false,//是否可折叠的
				fit: true,//自动大小
				method: "post",
				url:[[@{/cow/getCowList}]],
				idField:'id',
				singleSelect:false,//是否单选
				pagination:true,//分页控件
				rownumbers:true,//行号
				sortName:'id',
				sortOrder:'DESC',
				remoteSort: false,
				columns: [
				[
					{field:'chk',checkbox: true,width:50},
					{field:'id',title:'ID',width:50, sortable: true},
					{field:'sn',title:'编号',width:200, sortable: true},
					{field:'sex',title:'性别',width:100},
					{field:'age',title:'年龄',width:150},
					{field:'weight',title:'体重',width:150},
					{field:'category_id',title:'种类',width:150,
						formatter: function(value,row,index){
							if (row.categoryId){
								var categoryList = $("#categoryList").combobox("getData");
								for(var i=0;i<categoryList.length;i++ ){
									//console.log(categoryList[i]);
									if(row.categoryId == categoryList[i].id)return categoryList[i].name;
								}
								return row.categoryId;
							} else {
								return 'not found';
							}
						}
					},
				]
				],
				toolbar: "#toolbar",
				onBeforeLoad : function(){
					try{
						$("#categoryList").combobox("getData")
					}catch(err){
						preLoadCategory();
					}
				}
			});
			//设置分页控件
			var p = $('#dataList').datagrid('getPager');
			$(p).pagination({
				pageSize: 10,//每页显示的记录条数，默认为10
				pageList: [10,20,30,50,100],//可以设置每页记录条数的列表
				beforePageText: '第',//页数文本框前显示的汉字
				afterPageText: '页    共 {pages} 页',
				displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			});
			//设置工具类按钮
			$("#add").click(function(){
				$("#addDialog").dialog("open");
			});
			//修改
			$("#edit").click(function(){
				var selectRows = $("#dataList").datagrid("getSelections");
				if(selectRows.length != 1){
					$.messager.alert("消息提醒", "请选择一条数据进行操作!", "warning");
				} else{
					$("#editDialog").dialog("open");
				}
			});
			//删除
			$("#delete").click(function(){
				var selectRows = $("#dataList").datagrid("getSelections");
				var selectLength = selectRows.length;
				if(selectLength == 0){
					$.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
				} else{
					var jsonObj = {};
					$.each(selectRows,function(i,n){
						jsonObj["ids["+i+"]"] = n.id;
					});
					$.messager.confirm("消息提醒", "将删除与牛相关的所有数据，确认继续？", function(r){
						if(r){
							$.ajax({
								type: "post",
								url: [[@{/cow/deleteCow}]],
								data:jsonObj,
								success: function(data){
									if(data.success){
										$.messager.alert("消息提醒",data.message,"info");
										//刷新表格
										$("#dataList").datagrid("reload");
										$("#dataList").datagrid("uncheckAll");
									} else{
										$.messager.alert("消息提醒",data.message,"warning");
										return;
									}
								}
							});
						}
					});
				}
			});


			function preLoadCategory(){
				$("#categoryList").combobox({
					width: "150",
					height: "25",
					valueField: "id",
					textField: "name",
					multiple: false, //可多选
					editable: false, //不可编辑
					method: "post",
					url: [[@{/category/getCategoryList(from=combox)}]],
					onChange: function(newValue, oldValue){
						//加载班级下的牛
						//$('#dataList').datagrid("options").queryParams = {categoryid: newValue};
						//$('#dataList').datagrid("reload");
					}
				});
			}

			//下拉框通用属性
			$("#add_categoryList, #edit_categoryList").combobox({
				width: "200",
				height: "30",
				valueField: "id",
				textField: "name",
				multiple: false, //可多选
				editable: false, //不可编辑
				method: "post",
			});


			$("#add_categoryList").combobox({
				url: [[@{/category/getCategoryList(from=combox)}]],
				onLoadSuccess: function(){
					//默认选择第一条数据
					var data = $(this).combobox("getData");;
					$(this).combobox("setValue", data[0].id);
				}
			});



			$("#edit_categoryList").combobox({
				url: [[@{/category/getCategoryList(from=combox)}]],
				onLoadSuccess: function(){
					//默认选择第一条数据
					var data = $(this).combobox("getData");
					$(this).combobox("setValue", data[0].id);
				}
			});

			//设置添加牛窗口
			$("#addDialog").dialog({
				title: "添加牛",
				width: 650,
				height: 460,
				iconCls: "icon-add",
				modal: true,
				collapsible: false,
				minimizable: false,
				maximizable: false,
				draggable: true,
				closed: true,
				buttons: [
					{
						text:'添加',
						plain: true,
						iconCls:'icon-user_add',
						handler:function(){
							var validate = $("#addForm").form("validate");
							if(!validate){
								$.messager.alert("消息提醒","请检查你输入的数据!","warning");
								return;
							} else{
								var categoryid = $("#add_categoryList").combobox("getValue");
								var formData = new FormData($( "#addForm" )[0]);
								$.ajax({
									type: "post",
									url: [[@{/cow/addCow}]],
									data: formData,
									async: false,
									cache: false,
									contentType: false,
									processData: false,
									success: function(data){
										if(data.success){
											$.messager.alert("消息提醒",data.message,"info");
											//关闭窗口
											$("#addDialog").dialog("close");
											//清空原表格数据
											$("#add_sex").textbox('setValue', "雄");
											$("#add_age").textbox('setValue', "");
											$("#add_weight").textbox('setValue', "");

											//重新刷新页面数据
											$('#dataList').datagrid("options").queryParams = {categoryid: categoryid};
											$('#dataList').datagrid("reload");
											setTimeout(function(){
												$("#categoryList").combobox('setValue', categoryid);
											}, 100);

										} else{
											$.messager.alert("消息提醒",data.message,"warning");
											return;
										}
									}
								});
							}
						}
					},
					{
						text:'重置',
						plain: true,
						iconCls:'icon-reload',
						handler:function(){
							$("#add_age").textbox('setValue', "");
							$("#add_weight").textbox('setValue', "");
							// $("#add_sex").textbox('setValue', "雄");
						}
					},
				]
			});

			//设置编辑牛窗口
			$("#editDialog").dialog({
				title: "修改牛信息",
				width: 650,
				height: 460,
				iconCls: "icon-edit",
				modal: true,
				collapsible: false,
				minimizable: false,
				maximizable: false,
				draggable: true,
				closed: true,
				buttons: [
					{
						text:'提交',
						plain: true,
						iconCls:'icon-user_add',
						handler:function(){
							var validate = $("#editForm").form("validate");
							var categoryid = $("#edit_categoryList").combobox("getValue");
							if(!validate){
								$.messager.alert("消息提醒","请检查你输入的数据!","warning");
								return;
							} else{
								var formData = new FormData($( "#editForm" )[0]);
								$.ajax({
									type: "post",
									url: [[@{/cow/editCow}]],
									data: formData,
									async: false,
									cache: false,
									contentType: false,
									processData: false,
									success: function(data){
										if(data.success){
											$.messager.alert("消息提醒",data.message,"info");
											//关闭窗口
											$("#editDialog").dialog("close");
											//刷新表格
											$('#dataList').datagrid("options").queryParams = {categoryid: categoryid};
											$("#dataList").datagrid("reload");
											$("#dataList").datagrid("uncheckAll");

											setTimeout(function(){
												$("#categoryList").combobox('setValue', categoryid);
											}, 100);

										} else{
											$.messager.alert("消息提醒",data.message,"warning");
											return;
										}
									}
								});
							}
						}
					},
					{
						text:'重置',
						plain: true,
						iconCls:'icon-reload',
						handler:function(){
							//清空表单
							$("#edit_age").textbox('setValue', "");
							// $("#edit_sex").textbox('setValue', "雄");
							$("#edit_weight").textbox('setValue', "");
						}
					}
				],
				onBeforeOpen: function(){
					var selectRow = $("#dataList").datagrid("getSelected");
					$("#imgd div").remove();
					$.ajax({
						type: "post",
						url: [[@{/system/getPhoto}]]+"?sid="+selectRow.id,
						success: function(data){
							//设置值
							$("#edit_age").textbox('setValue', selectRow.age);
							$("#edit_sex").textbox('setValue', selectRow.sex);
							$("#edit_weight").textbox('setValue', selectRow.weight);

							$("#edit-id").val(selectRow.id);
							var categoryid = selectRow.categoryId;
							setTimeout(function(){
								$("#edit_categoryList").combobox('setValue', categoryid);
							}, 100);
						}
					});
				}
			});
			//搜索按钮监听事件
			$("#search-btn").click(function(){
				$('#dataList').datagrid('load',{
					cowsn: $('#search_cow_sn').val(),
					categoryid: $("#categoryList").combobox('getValue') == '' ? 0 : $("#categoryList").combobox('getValue')
				});
			});
		});
	</script>
</head>
<body>
<!-- 牛列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">

</table>
<!-- 工具栏 -->
<div id="toolbar">
	<div th:if="${session.usertype == '1'}" style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a></div>
	<div th:if="${session.usertype == '1'}" style="float: left;" class="datagrid-btn-separator"></div>
	<div th:if="${session.usertype == '1'}" style="float: left;"><a id="edit" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a></div>
	<div th:if="${session.usertype == '1'}" style="float: left;" class="datagrid-btn-separator"></div>
	<div th:if="${session.usertype == '1'}" style="float: left;"><a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>
	<div th:if="${session.usertype == '1'}" style="float: left;margin-top:4px;" class="datagrid-btn-separator" >&nbsp;&nbsp;编号：<input id="search_cow_sn" class="easyui-textbox" name="search_cow_sn" /></div>
	<div style="margin-left: 10px;margin-top:4px;" >种类：<input id="categoryList" class="easyui-textbox" name="category" />
	<a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
	</div>

</div>

<!-- 添加牛窗口 -->
<div id="addDialog" style="padding: 10px">
	<form id="addForm" method="post" enctype="multipart/form-data">
		<table cellpadding="8" >
			<tr>
				<td>性别:</td>
				<td><select id="add_sex" class="easyui-combobox" data-options="editable: false, panelHeight: 50, width: 60, height: 30" name="sex"><option value="雄">雄</option><option value="雌">雌</option></select></td>
			</tr>
			<tr>
				<td>年龄:</td>
				<td><input id="add_age" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="age" validType="age" /></td>
			</tr>
			<tr>
				<td>种类:</td>
				<td><input id="add_categoryList" style="width: 200px; height: 30px;" class="easyui-textbox" name="categoryId" /></td>
			</tr>
			<tr>
				<td>体重:</td>
				<td><input id="add_weight" style="width: 200px; height: 30px;" class="easyui-textbox" name="weight" validType="weight"/></td>
			</tr>
		</table>
	</form>
</div>

<!-- 修改牛窗口 -->
<div id="editDialog" style="padding: 10px">
	<form id="editForm" method="post" enctype="multipart/form-data">
		<input type="hidden" name="id" id="edit-id">
		<table cellpadding="8" >
			<tr>
				<td>性别:</td>
				<td><select id="edit_sex" class="easyui-combobox" data-options="editable: false, panelHeight: 50, width: 60, height: 30" name="sex"><option value="男">男</option><option value="女">女</option></select></td>
			</tr>
			<tr>
				<td>年龄:</td>
				<td><input id="edit_age" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="age" validType="age" /></td>
			</tr>
			<tr>
				<td>种类:</td>
				<td><input id="edit_categoryList" style="width: 200px; height: 30px;" class="easyui-textbox" name="categoryId" /></td>
			</tr>
			<tr>
				<td>体重:</td>
				<td><input id="edit_weight" style="width: 200px; height: 30px;" class="easyui-textbox" name="weight" /></td>
			</tr>
		</table>
	</form>
</div>
</body>
</html>